﻿module program

open Perceptron
open System
open System.IO
open ScriptMonad
open MathNet.Numerics
open MathNet.Numerics.FSharp
open MathNet.Numerics.LinearAlgebra.Double

let stream = File.Open("..\..\data\iris.data",FileMode.Open, FileAccess.Read)
let stream_reader = new StreamReader(stream) 
let text = stream_reader.ReadToEnd()
stream_reader.Close()
stream.Close()


let labels = Map.ofList([("Iris-setosa",-1.); ("Iris-versicolor",-1.); ("Iris-virginica",1.)])

let input =
  #if KD
    let splitted_text = text.Split(',','\n')  |> Seq.filter(fun elem -> elem <> "") |> Seq.toList
    let rec build_vectors list =
      match list with
      [] -> []
      |(x1::x2::x3::x4::x5 :: xs) -> 
        let point = new DenseVector([|float x1; float x2; float x3; float x4; 1.0|])
        let misscounter = 0
        let label = labels.[x5]
        PerceptronPoint(point, ref misscounter, label) :: build_vectors xs
    

    build_vectors splitted_text
  #else
  [new DenseVector([|50.;99.;1.|]), ref 0, -1.; 
         new DenseVector([|301.;190.;1.|]), ref 0, 1.;
         new DenseVector([|200.;200.;1.|]), ref 0, -1.;
         new DenseVector([|100.;250.;1.|]), ref 0, -1.;
         new DenseVector([|150.;50.;1.|]), ref 0, 1.;
         new DenseVector([|10.;20.;1.|]), ref 0, 1.;  ]
  #endif
       
let w = 
  #if KD
    new DenseVector([|0.;0.;0.;0.;0.|])
  #else
    new DenseVector([|1.;1.;1.|])
  #endif


let program() = 
  let compute_perceptron_script = compute_perceptron input w |> ref

  let rec step_perceptron_script() =
      match !compute_perceptron_script () with
      | Return w' -> w'
      | Yield k | ArrowYield k -> 
        do compute_perceptron_script := k
        step_perceptron_script()

  step_perceptron_script()

let final_w = program()

